﻿@model MessageTemplateListLocalModel
@using Nop.Plugin.Misc.MessageDesigner.Models;
@using System.Globalization;
@using Nop.Core.Infrastructure;
@using Nop.Web.Framework;
@using Nop.Web.Framework.UI;
@{
    //Layout = "~/Administration/Views/Shared/_AdminLayout.cshtml";
    //page title
    ViewBag.Title = T("Admin.ContentManagement.MessageTemplates").Text;
}
@using (Html.BeginForm())
{
    @Html.HiddenFor(model => model.MessageDesignId)
    <div class="section-header">
        <div class="title">
            <img src="@Url.Content("~/Administration/Content/images/ico-content.png")" alt="" />
            @T("Admin.ContentManagement.MessageTemplates")
        </div>
        <div class="options">
            <input type="button" id="apply-to-selected" class="k-button" value="@T("Plugins.Misc.MessageDesigner.ApplyToselected")" />
        </div>
    </div>
    <table width="100%">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchStoreId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchStoreId", Model.AvailableStores)
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" id="search-templates" class="k-button" value="@T("Admin.Common.Search")" />
            </td>
        </tr>
    </table>
    <table class="adminContent">
        <tr>
            <td>
                <div id="templates-grid"></div>

                <script>
    $(document).ready(function () {
        $("#templates-grid").kendoGrid({
            dataSource: {
                type: "json",
                transport: {
                    read: {
                        url: "@Html.Raw(Url.Action("ListTemplates", "MiscMessageDesigner"))",
                        type: "POST",
                        dataType: "json",
                        data: additionalData
                    }
                },
                schema: {
                    data: "Data",
                    total: "Total",
                    errors: "Errors"
                },
                error: function (e) {
                    display_kendoui_grid_error(e);
                    // Cancel the changes
                    this.cancelChanges();
                },
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true
            },
            pageable: {
                refresh: true,
                numeric: false,
                previousNext: false,
                info: false
            },
            editable: {
                confirmation: false,
                mode: "inline"
            },
            scrollable: false,
            dataBound: onDataBound,
            columns: [{
                field: "Id",
                headerTemplate: "<input id='mastercheckbox' type='checkbox'/>",
                headerAttributes: { style: "text-align:center" },
                attributes: { style: "text-align:center" },
                template: "<input type='checkbox' value='#=Id#' class='checkboxGroups'/>",
                width: 50
            }, {
                field: "Name",
                title: "@T("Admin.ContentManagement.MessageTemplates.Fields.Name")"
            }, {
                field: "Subject",
                title: "@T("Admin.ContentManagement.MessageTemplates.Fields.Subject")"
            }, {
                field: "IsActive",
                title: "@T("Admin.ContentManagement.MessageTemplates.Fields.IsActive")",
                width: 100,
                headerAttributes: { style: "text-align:center" },
                attributes: { style: "text-align:center" },
                template: '<img src="@Url.Content("~/Administration/Content/images/")active-#=IsActive#.gif" />'
            }, {
                field: "DesignName",
                title: "@T("Plugins.Misc.MessageDesigner.Fields.DesignName")"
            }, {
                field: "Id",
                title: "@T("Plugins.Misc.MessageDesigner.Fields.View")",
                width: 100,
                template: '<a  onclick="javascript:OpenWindow(\'@(Url.Action("ViewTemplate"))?templateId=#=Id#\', 800, 800, true); return false;" >@T("Plugins.Misc.MessageDesigner.Fields.View")</a>'
            }, {
                field: "Id",
                title: "@T("Admin.Common.Edit")",
                                width: 100,
                                template: '<a href="@Url.Content("~/MessageTemplate/Edit/")#=Id#">@T("Admin.Common.Edit")</a>'
            }]
        });
    });
                </script>

                <script type="text/javascript">
                    var selectedIds = [];

                    $(document).ready(function () {
                        //search button
                        $('#search-templates').click(function () {
                            //search
                            var grid = $('#templates-grid').data('kendoGrid');
                            grid.dataSource.read();
                            //clear selected checkboxes
                            $('.checkboxGroups').attr('checked', false).change();
                            selectedIds = [];
                            return false;
                        });

                        $('#mastercheckbox').click(function () {
                            $('.checkboxGroups').attr('checked', $(this).is(':checked')).change();
                        });

                        //wire up checkboxes.
                        $('#templates-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function (e) {
                            var $check = $(this);
                            if ($check.is(":checked") == true) {
                                var checked = jQuery.inArray($check.val(), selectedIds);
                                if (checked == -1) {
                                    //add id to selectedIds.
                                    selectedIds.push($check.val());
                                }
                            }
                            else {
                                var checked = jQuery.inArray($check.val(), selectedIds);
                                if (checked > -1) {
                                    //remove id from selectedIds.
                                    selectedIds = $.grep(selectedIds, function (item, index) {
                                        return item != $check.val();
                                    });
                                }
                            }
                            updateMasterCheckbox();
                        });
                    });

                    function additionalData() {
                        return {
                            SearchStoreId: $('#SearchStoreId').val(),
                            MessageDesignId: $('#MessageDesignId').val()
                        };
                    }

                    function onDataBound(e) {
                        $('#templates-grid input[type=checkbox][id!=mastercheckbox]').each(function () {
                            var currentId = $(this).val();
                            var checked = jQuery.inArray(currentId, selectedIds);
                            //set checked based on if current checkbox's value is in selectedIds.
                            $(this).attr('checked', checked > -1);
                        });

                        updateMasterCheckbox();
                    }

                    function updateMasterCheckbox() {
                        var numChkBoxes = $('#templates-grid input[type=checkbox][id!=mastercheckbox]').length;
                        var numChkBoxesChecked = $('#templates-grid input[type=checkbox][checked][id!=mastercheckbox]').length;
                        $('#mastercheckbox').attr('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
                    }
                </script>
            </td>
        </tr>
    </table>
}

@*export selected (XML). We don't use GET approach because it's limited to 2K-4K chars and won't work for large number of entities*@
@using (Html.BeginForm("ApplyToSelected", "MiscMessageDesigner", FormMethod.Post, new { id = "apply-to-selected-form" }))
{
    <input type="hidden" id="selectedIds" name="selectedIds" value="" />
    <input type="hidden" id="designId" name="designId" value="@Model.MessageDesignId" />
}
<script type="text/javascript">

    $(document).ready(function () {
        $('#apply-to-selected').click(function (e) {
            e.preventDefault();
            var ids = selectedIds.join(",");
            $('#apply-to-selected-form #selectedIds').val(ids);
            $('#apply-to-selected-form').submit();
            return false;
        });
    });

</script>